   
package com.mycjj.android.activity;

import java.util.Calendar;
import java.util.List;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mycjj.android.R;
import com.mycjj.android.adapter.TAdapter;
import com.mycjj.android.bean.BrandBaseBean;
import com.mycjj.android.bean.BrandBaseBean.Brand;
import com.mycjj.android.bean.TreeElement;
import com.mycjj.android.http.ProjectHttpRequestImpl;
import com.mycjj.android.util.Utils;


public class BrandActivity extends BaseActivity{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_brand);
		initHeader();
		init();
		
		initData(0,"");
	}
	
	private ImageView mHeaderLeftImageview;
	private Button mHeaderRightButton;
	private TextView mHeaderTitleTextView;

	private void initHeader() {
		mHeaderLeftImageview = (ImageView) findViewById(R.id.header_left_button);
		mHeaderLeftImageview.setImageResource(R.drawable.back);
		mHeaderRightButton = (Button) findViewById(R.id.header_right_button);
		mHeaderRightButton.setVisibility(View.GONE);
		mHeaderTitleTextView = (TextView) findViewById(R.id.header_title_textview);
		mHeaderTitleTextView.setText("车辆品牌");
		mHeaderLeftImageview.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				back();
			}
		});
	}
	
	private void back(){
		if(listview04.getVisibility() == View.VISIBLE){
			listview03.setVisibility(View.VISIBLE);
			listview04.setVisibility(View.GONE);
		}else if(listview03.getVisibility() == View.VISIBLE){
			listview02.setVisibility(View.VISIBLE);
			listview03.setVisibility(View.GONE);
			
		}else if(listview02.getVisibility() == View.VISIBLE){
			listview01.setVisibility(View.VISIBLE);
			listview02.setVisibility(View.GONE);
		}else if(listview01.getVisibility() == View.VISIBLE){
			BrandActivity.this.finish();
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			back();
		}
		return super.onKeyDown(keyCode, event);
	}
	
	private ListView listview01,listview02,listview03,listview04;
	private BrandAdapter adapter1,adapter2,adapter3,adapter4; 
	private String brandId1,brandId2,brandId3,brandId4;
	private String brandName;
	private String brandIcon;
	private void init(){
		listview01 = (ListView)findViewById(R.id.listview1);
		listview02 = (ListView)findViewById(R.id.listview2);
		listview03 = (ListView)findViewById(R.id.listview3);
		listview04 = (ListView)findViewById(R.id.listview4);
		
		adapter1 = new BrandAdapter(this,true);
		adapter2 = new BrandAdapter(this,false);
		adapter3 = new BrandAdapter(this,false);
		adapter4 = new BrandAdapter(this,false);
		
		listview01.setAdapter(adapter1);
		listview02.setAdapter(adapter2);
		listview03.setAdapter(adapter3);
		listview04.setAdapter(adapter4);
		
		listview01.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				Brand brand = data1.get(position);
				brandId1 = brand.getId();
				Log.e("mark", "一级品牌目录id:"+brandId1);
				brandIcon = brand.getIcon();
				initData(1,brandId1);
			}
		});
		
		listview02.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				Brand brand = data2.get(position);
				brandId2 = brand.getId();
				initData(2,brandId2);
			}
		});
		
		
		listview03.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				Brand brand = data3.get(position);
				brandId3 = brand.getId();
				initData(3,brandId3);
			}
		});
		
		
		listview04.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				Brand brand = data4.get(position);
				brandId4 = brand.getId();
				brandName = brand.getName();
				showDialog(DATE_DIALOG_ID);
			}
		});
		
		final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
		
	}
	static final int DATE_DIALOG_ID = 1;
	private int mYear;
	private int mMonth;
	private int mDay;
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DATE_DIALOG_ID:
			DatePickerDialog picker = new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
					mDay);
			picker.setTitle("请选择车辆购买日期");
			return picker;
		}
		return null;
	}

	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			mYear = year;
			mMonth = monthOfYear;
			mDay = dayOfMonth;
			Log.e("mark", "year:"+year+";monthOfYear:"+monthOfYear+";dayOfMonth:"+dayOfMonth);
			String time = year+"-"+Utils.pad(monthOfYear+1)+"-"+Utils.pad(dayOfMonth);
			TreeElement treeElement = new TreeElement();
			treeElement.setTitle(brandName);
			treeElement.setBrandId(brandId1);
			treeElement.setBrandId1(brandId2);
			treeElement.setBrandId2(brandId3);
			treeElement.setBrandId3(brandId4);
			Log.e("mark", "brandName:"+brandName+";brandId1:"+brandId1+";brandId2:"+brandId2+";brandId3:"+brandId3+";brandId4:"+brandId4);
			treeElement.setBuyCarDate(time);
			dataManager.setElement(treeElement);
			BrandActivity.this.finish();
		}
	};
	
	private String id;
	private List<Brand> data1,data2,data3,data4;
	private void initData(final int type,String id){
		request = new ProjectHttpRequestImpl(this,true);
		RequestParams params  = new RequestParams();
		if(!TextUtils.isEmpty(id)){
			params.put("id", id);
		}
		request.requestUserCarGetBrand("", params, new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(String content) {
				BrandBaseBean baseBean = dataManager.getBrandBaseBean();
				if(baseBean != null){
					if(baseBean.getCode() == 200){
						switch(type){
						case 0:
							data1 = baseBean.getData();
							adapter1.setList(data1);
							break;
						case 1:
							data2 = baseBean.getData();
							adapter2.setList(data2);
							listview01.setVisibility(View.GONE);
							listview02.setVisibility(View.VISIBLE);
							break;
						case 2:
							data3 = baseBean.getData();
							adapter3.setList(data3);
							listview02.setVisibility(View.GONE);
							listview03.setVisibility(View.VISIBLE);
							break;
						case 3:
							data4 = baseBean.getData();
							adapter4.setList(data4);
							listview03.setVisibility(View.GONE);
							listview04.setVisibility(View.VISIBLE);
							break;
						}
					}
				}else{
					
				}
				
			}
		});
	}
	
	
	
	public class BrandAdapter extends TAdapter<Brand>{

		private boolean isShow;
		public BrandAdapter(Context mContext,boolean isShow) {
			super(mContext);
			this.isShow = isShow;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(R.layout.item_brand_view, null);
				holder = new ViewHolder();
				holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
				holder.pic = (ImageView)convertView.findViewById(R.id.header_pic);
				holder.view = convertView.findViewById(R.id.fl_header_pic);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			Brand bean = mList.get(position);
			if(isShow){
				holder.view.setVisibility(View.VISIBLE);
				imageLoader.displayImage(bean.getIcon(), holder.pic);
			}else{
				holder.view.setVisibility(View.GONE);
			}
			holder.tv_name.setText(bean.getName());
			return convertView;
		}
		
		class ViewHolder{
			TextView tv_name;
			ImageView pic;
			View view;
		}
		
		

	}
	
	
	

	
	
}
